$(function () {
    //应收票据
    var ysColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'pjlx', title: '票据类型', width: 10, align: 'center'},
            {field: 'pjhm', title: '票据号码', width: 15, align: 'center'},
            {field: 'sfkbm', title: '收款号码', width: 15, align: 'center'},
            {field: 'cpyh', title: '出票银行', width: 25, align: 'center'},
            {field: 'cprq', title: '出票日', width: 15, align: 'center'},
            {field: 'dqr', title: '到期日', width: 15, align: 'center'},
            {field: 'dqje', title: '到期金额', width: 20, align: 'center', formatter: formatSlForHtml2},
            {field: 'cpr', title: '出票人', width: 25, align: 'center'},
            {field: 'spr', title: '收款人', width: 25, align: 'center'},
            {field: 'jpdw', title: '交款单位', width: 25, align: 'center'},
            {field: 'syrq', title: '使用日期', width: 10, align: 'center'},
            {field: 'bz', title: '备注', width: 10, align: 'center'},
            {field: 'txcdh', title: '贴现承兑号', width: 15, align: 'center'},
            {field: 'rzyh', title: '入账银行', width: 10, align: 'center'}
        ]
    ];

    //应付票据
    var yfColumns = [
        [
            {field: 'gsmc', title: '公司名称', width: 25, align: 'center'},
            {field: 'pjlx', title: '票据类型', width: 10, align: 'center'},
            {field: 'pjhm', title: '票据号码', width: 15, align: 'center'},
            {field: 'sfkbm', title: '付款号码', width: 15, align: 'center'},
            {field: 'cpyh', title: '出票银行', width: 25, align: 'center'},
            {field: 'cprq', title: '出票日', width: 15, align: 'center'},
            {field: 'dqr', title: '到期日', width: 15, align: 'center'},
            {field: 'dqje', title: '金额', width: 20, align: 'center', formatter: formatSlForHtml2},
            {field: 'bzj', title: '保证金', width: 20, align: 'center', formatter: formatSlForHtml2},
            {field: 'yflx', title: '应付利息', width: 20, align: 'center', formatter: formatSlForHtml2},
            {field: 'spr', title: '收票人', width: 25, align: 'center'},
            {field: 'jpdw', title: '交款单位', width: 25, align: 'center'},
            {field: 'syrq', title: '使用日期', width: 10, align: 'center'},
            {field: 'bz', title: '备注', width: 10, align: 'center'}
        ]
    ];
    //===============================================
    $("#pjkc_detail_toolbar [name='gsId']").gsjgtree(1);
    //计划开始时间
    $("#pjkc_detail_toolbar [name='startDate'] :text").datebox({
        required: true
    });
    //计划结束时间
    $("#pjkc_detail_toolbar [name='endDate']").datebox({
        required: true
    });

    var columns = null;
    var sftype = $("#pjkc_detail_toolbar [name='sftype']").val();
    switch (sftype) {
        case "0":
            columns = ysColumns;
            break;
        case "1":
            columns = yfColumns;
            break;
        default:
            columns = [
                []
            ];
    }

    //表格初始加载
    var grid = $('#pjkc_detail_grid').datagrid({
        url: $.webPath() + "/report/pjkc/detailList",
        toolbar: "#pjkc_detail_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        pageNumber: 1,
        striped: true,
        showFooter: true,
        queryParams: {
            startDate: $("#pjkc_detail_toolbar [name='startDate']").val(),
            endDate: $("#pjkc_detail_toolbar [name='endDate']").val(),
            sftype: $("#pjkc_detail_toolbar [name='sftype']").val(),
            type: $("#pjkc_detail_toolbar [name='type']").val(),
            gsId: $("#pjkc_detail_toolbar [name='gsId']").val(),
            status: $("#pjkc_detail_toolbar [name='status']").val()
        },
        columns: columns
    });

    //搜索按钮
    $("#pjkc_detail_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                startDate: $("#pjkc_detail_toolbar [name='startDate']").val(),
                endDate: $("#pjkc_detail_toolbar [name='endDate']").val(),
                sftype: $("#pjkc_detail_toolbar [name='sftype']").val(),
                type: $("#pjkc_detail_toolbar [name='type']").val(),
                gsId: $("#pjkc_detail_toolbar [name='gsId']").val(),
                status: $("#pjkc_detail_toolbar [name='status']").val()
            }});
    });
    //导出
    $("#pjkc_detail_toolbar [name='export']").click(function () {
        var url=$.webPath() + "/report/pjkc/exportDetail?"
            + "&sftype=" + $("#pjkc_detail_toolbar [name='sftype']").val()
            + "&type=" + $("#pjkc_detail_toolbar [name='type']").val()
            + "&gsId=" + $("#pjkc_detail_toolbar [name='gsId']").val()
            + "&status=" + $("#pjkc_detail_toolbar [name='status']").val();

        if($("#pjkc_detail_toolbar [name='startDate']").val()){
            url+="&startDate="+$("#pjkc_detail_toolbar [name='startDate']").val();
        }
        if($("#pjkc_detail_toolbar [name='endDate']").val()){
            url+="&endDate="+$("#pjkc_detail_toolbar [name='endDate']").val();
        }
        window.open(url);
    });
});